<?php

namespace Home\Controller;
use Common\Controller\HomecommController;
class PostController extends HomecommController{
    
    /**
     * 获取验证码
     */
    public function getCode()
    {   
        $reg = I('post.reg',0);//注册时发送验证码标志
        $phone = I('post.username');
        if($reg == 1){
            $member = M('Member')->where(array('username'=>$phone))->find();
            if($member){
                echo json_encode(array('err'=>1,'msg'=>'手机号已存在'));exit;
            }
            $text = '【读财社】感谢您的注册，您的验证码是".$random."，五分钟内使用有效，如非本人操作，请忽略此条短信。';
        }
        $text = '【读财社】您的验证码是".$random."，五分钟内使用有效，如非本人操作，请忽略此条短信。';
        $sms = new \Api\Controller\SmsController();
		$random = rand(100000,999999);
        $md5 = md5('jinrongkuanggong'.$random);
        session("verifycode" , $md5);
        echo json_encode(array('err'=>0,'msg'=>$random));exit;
         $key = rand(100000,999999);
        $keymd5 = md5('jinrongkuanggong'.$key);
        session("CheckKey" , $keymd5);
        $result = $sms->sendSMS($mobile, $text, 'true',$key);
        //添加短信日志
        D('Sms_log')->addlog($phone,$text,json_encode($result));
        if(isset($result[1]) && $result[1]==0){
            echo json_encode(array('err'=>0,'msg'=>'OK'));
            die;
        }else{
            echo json_encode(array('err'=>1,'msg'=>'重新发送'));
            die;
        }
    }
    
    /**
     * 收藏
     */
    public function collect()
    {
        $carId = I('Post.carid');
        if(!$carId){
            echo json_encode(array('err'=>1,'msg'=>'非法操作'));exit;
        }
        if(!session('user_id')){
            echo json_encode(array('err'=>1,'msg'=>'请先登录'));exit;
        }
        $collectModel = M('Collect');
        $data['CarId'] = $carId;
        $data['CusId'] = session('user_id');
        $car = $collectModel->where($data)->find();
        if($car){//已收藏
            echo json_encode(array('err'=>1,'msg'=>'您已收藏'));exit;
        }else{
            $data['CollectDate'] = date('Y-m-d H:i:s',time());
            $data['Id'] = create_guid();
            $res = $collectModel->add($data);
            if(!$res){
                echo json_encode(array('err'=>1,'msg'=>'收藏失败'));exit;
            }else{
                echo json_encode(array('err'=>0,'msg'=>'收藏成功'));exit;
            }
        }
    }
    /**
     * 取消收藏
     */
    public function collectClear()
    {
        $collect_id = I('Post.collect_id');
        if(!$collect_id){
            echo json_encode(array('err'=>1,'msg'=>'非法操作'));exit;
        }
        if(!session('user_id')){
            echo json_encode(array('err'=>1,'msg'=>'请先登录'));exit;
        }
        $collectModel = M('Collect');
        $data['Id'] = $collect_id;
        $data['CusId'] = session('user_id');
        $car = $collectModel->where($data)->find();
        if(!$car){//已收藏
            echo json_encode(array('err'=>1,'msg'=>'您未收藏此车'));exit;
        }else{
            $res = $collectModel->where($data)->delete();
            if(!$res){
                echo json_encode(array('err'=>1,'msg'=>'取消收藏失败'));exit;
            }else{
                echo json_encode(array('err'=>0,'msg'=>'取消收藏成功'));exit;
            }
        }
    }
    /**
     * 图片验证码
     */
    public function img_code()
    {
        ob_clean();//丢弃输出缓冲区中的内容
        
        $config = array(
            'fontSize'    =>    20,    // 字体大小
            'length'      =>    4,     // 验证码位数
            'useCurve'    =>    false, // 开关验证码杂点 
            // 'useImgBg'    =>    true   //图片背景图
        ); 
        $Verify = new \Think\Verify($config);
        $Verify->codeSet = '0123456789';//defghijklmnopqrstvuw
        $Verify->entry();
        // <img width="100" height="43" src="/images/默认图片.jpg"  onClick="this.src='/Home/login/verify?'+Math.random();" >
    }
    /**
     * 图片验证码 验证
     */
    function check_verify(){
        $img_code = I('post.img_code');
        $verify = new \Think\Verify();
        echo $verify->check($img_code);exit;
    }
}
